/********************************************************************************
 * @file    atcmd_slave.c
 * @author  jianqiang.xue
 * @Version V1.0.0
 * @Date    2022-09-04
 * @brief   从机版 AT指令 https://lisun.blog.csdn.net/article/details/126683930
 ********************************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#include "atcmd_slave.h"

extern atcmd_info_t __atcmd_start;
extern atcmd_info_t __atcmd_end;

bool atcmd_msg_handle(atcmd_pack_t* pack) {
    bool match = false;
    atcmd_info_t* atcmd;
    for (atcmd = &__atcmd_start; atcmd < &__atcmd_end; atcmd++) {
      if (atcmd->name != (char *)0xFFFFFFFF) { // 针对FLASH默认值为0xFFFFFFFFF时，不进行比较
        if (strncmp((char*)(pack->data), atcmd->name, strlen(atcmd->name)) == 0) {
            match = true;
            break;
        }
      }
    }
    if (match) {
        // 裁剪  AT^XXXX=(保留)\r\n
        pack->len -= strlen(atcmd->name) - 2; // 3 == \r\n
        pack->data += strlen(atcmd->name); // 移除前面的内容,并包含'=' or '?' or '=?'
        pack->data[pack->len + 1] = '\0';
        atcmd->callback(pack);
        return 0;
    }
    return -1;
}

